package com.recursiveflow.openplatform.view 
{
    import com.guardian.apis.content.model.Tag;
    import com.guardian.apis.content.model.TagType;

    import flash.geom.ColorTransform;
    import flash.utils.Dictionary;

    /**
     * Determines tag colour based on its type and the presence of certain keywords in its full name.
     * 
     * @author Alastair Dant / Recursive Flow / 2009
     */
    public class TagColourer 
    {
        private var colors : Dictionary;
        
        public function TagColourer() 
   		{
   			colors = new Dictionary();	

            addColour("default", 0x00417b);
            addColour("news", 0xd61d00);
            addColour("sport", 0x008000);
            addColour("football", 0x008000);
            addColour("business", 0x4a64d9);
            addColour("travel", 0x066ec9);
            addColour("politics", 0x9c1b00);
            addColour("environment", 0x4a7801);
            addColour("culture", 0xa7006f);
            addColour("music", 0xa7006f);
            addColour("books", 0xa7006f);
            addColour("film", 0xa7006f);
            addColour("art", 0xa7006f);
            addColour("tv", 0x7d0053);
            addColour("television", 0x7d0053);
            addColour("life and style", 0xc93900);
            addColour("money", 0x8f1ab6);
            addColour("contributor", 0xffa70e);
        }
        
        private function addColour(name : String, code : int) : void
        {
        	colors[name] = new ColorTransform();
        	colors[name].color = code;
        }
   		
        public function getColorFor(tag : Tag) : ColorTransform
        {
            switch (tag.getType())
            {
                case TagType.CONTRIBUTOR:	
                    return colors["contributor"] as ColorTransform;
                default:					
                    return lookupColorFor(tag.getFilter().toLowerCase() + " " + tag.getName().toLowerCase());
            }
        }

        private function lookupColorFor(name : String) : ColorTransform
        {
            var color : ColorTransform = colors["default"] as ColorTransform;
        	
            for (var section : String in colors)
            {
                if (name.toLowerCase().indexOf(section) >= 0) 
                { 
                    color = colors[section] as ColorTransform;	
                }	
            }

            return color;
        }   			
    }
}
